package org.map;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class map {
    public static void main(String[] args) {
        HashMap<Object, Object> map = new HashMap<>();
        map.put(1, "wsq");
        map.put(2, "wsq");
        map.put(3, "aaa");
        map.put(4, "bbb");
        System.out.println(map);
//        key相同等价于替换
        map.put(1, "sss");
        System.out.println(map);
        System.out.println(map.get(1));
        System.out.println("--------1---------");
//        遍历
        Set<Object> key = map.keySet();
        Iterator<Object> iterator = key.iterator();
        while (iterator.hasNext()) {
            Object next = iterator.next();
            System.out.println(next + "-" + map.get(next));
        }
        System.out.println("--------2---------");
        for (Object o : key) {
            System.out.println(o + "-" + map.get(o));
        }
        System.out.println("---------3--------");
        Set<Map.Entry<Object, Object>> entries = map.entrySet();
        Iterator<Map.Entry<Object, Object>> iterator1 = entries.iterator();
        while (iterator1.hasNext()) {
            Map.Entry<Object, Object> next = iterator1.next();
            Map.Entry m = (Map.Entry) next;
            System.out.println(next.getKey() + "-" + next.getValue());
        }
        System.out.println("--------4---------");
        for (Map.Entry<Object, Object> entry : entries) {
            Map.Entry m = (Map.Entry) entry;
            System.out.println(m.getKey() + "-" + m.getValue());
        }
    }
}
